import sys
import os
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QStackedWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QMenuBar, QFileDialog, QMessageBox
from PyQt5.QtCore import Qt, QTimer
from data.loader import load_json, load_heroes, load_battles, load_text, save_message
from views.pages.heroes import HeroesView
from views.pages.battles import BattlesView
from views.pages.text_scroller import TextScroller
from views.pages.messages import MessagesView


class MainWindow(QMainWindow):
    def __init__(self, data_path: str = "./data/data.json"):
        super().__init__()
        self.setWindowTitle("红色主题可视化 · PyQt5")
        self.resize(1200, 800)

        # Theme
        self.setStyleSheet(
            """
            QMainWindow { background: #160b0b; }
            QLabel { color: #f8f8f2; font-family: 'SimSun', 'Noto Sans SC', 'Microsoft YaHei UI'; }
            QPushButton { background: #2a1515; color: #f8f8f2; border: 1px solid #b71c1c; padding: 6px 12px; }
            QPushButton:hover { background: #3a1c1c; }
            QPushButton:pressed { background: #4a2121; }
            QMenuBar { background: #b71c1c; color: #ffffff; }
            QMenuBar::item:selected { background: #8e1616; }
            """
        )

        # Data
        data = load_json(data_path)
        self.heroes = load_heroes(data)
        self.battles = load_battles(data)
        self.spirit_text = load_text("./data/spirit.txt")
        self.peace_text = load_text("./data/peace.txt")

        # Menu
        menubar = self.menuBar()
        file_menu = menubar.addMenu("文件")
        open_action = file_menu.addAction("打开数据...")
        open_action.triggered.connect(self._open_data)

        # Central
        central = QWidget()
        self.setCentralWidget(central)
        layout = QVBoxLayout(central)

        # Navigation bar
        nav = QHBoxLayout()
        self.btn_heroes = QPushButton("英雄人物")
        self.btn_battles = QPushButton("重大战役")
        self.btn_spirit = QPushButton("抗战精神")
        self.btn_peace = QPushButton("和平愿景")
        self.btn_messages = QPushButton("青年留言")
        for b in [self.btn_heroes, self.btn_battles, self.btn_spirit, self.btn_peace, self.btn_messages]:
            nav.addWidget(b)
        layout.addLayout(nav)

        # Pages
        self.pages = QStackedWidget()
        layout.addWidget(self.pages, 1)

        self.page_heroes = HeroesView(self.heroes)
        self.page_battles = BattlesView(self.battles)
        self.page_spirit = TextScroller(self.spirit_text or "抗战精神内容加载失败。")
        self.page_peace = TextScroller(self.peace_text or "和平愿景内容加载失败。")
        self.page_messages = MessagesView()

        self.pages.addWidget(self.page_heroes)
        self.pages.addWidget(self.page_battles)
        self.pages.addWidget(self.page_spirit)
        self.pages.addWidget(self.page_peace)
        self.pages.addWidget(self.page_messages)

        # Connections
        self.btn_heroes.clicked.connect(lambda: self.pages.setCurrentWidget(self.page_heroes))
        self.btn_battles.clicked.connect(lambda: self.pages.setCurrentWidget(self.page_battles))
        self.btn_spirit.clicked.connect(lambda: self.pages.setCurrentWidget(self.page_spirit))
        self.btn_peace.clicked.connect(lambda: self.pages.setCurrentWidget(self.page_peace))
        self.btn_messages.clicked.connect(lambda: self.pages.setCurrentWidget(self.page_messages))

    def _open_data(self):
        path, _ = QFileDialog.getOpenFileName(self, "选择数据文件", "", "JSON Files (*.json)")
        if path:
            try:
                data = load_json(path)
                self.heroes = load_heroes(data)
                self.battles = load_battles(data)
                self.page_heroes.set_data(self.heroes)
                self.page_battles.set_data(self.battles)
                QMessageBox.information(self, "成功", "数据已加载。")
            except Exception as e:
                QMessageBox.critical(self, "错误", str(e))


